<template>
  <div class="login-container">
    <el-card class="login-box" shadow="always" style="width: 600px; box-shadow: none;">
      <div class="login-header">
        <h2>系统登录</h2>
      </div>
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        class="login-form"
        status-icon
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="User"
            class="login-input"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            show-password
            prefix-icon="Lock"
            class="login-input"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            class="login-btn"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { ref, reactive } from "vue";
import { ElMessage } from 'element-plus';

const router = useRouter();

const loginFormRef = ref(null);
const loginForm = reactive({
  username: "",
  password: "",
});

const loginRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid && loginForm.username && loginForm.password) {
      sessionStorage.setItem('isLogin', 'true');
      router.push({
        path: '/home',
      });
    } else {
      ElMessage.error("请正确填写登录信息");
    }
  });
};
</script>

<style scoped lang="less">
.login-container {
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #00bfff 0%, #1e90ff 100%);
  //background-image: url('/src/assets/images/login/bg.png'); /* 替换为您的图片路径 */
  background-size: cover;
  overflow: hidden;

  .login-box {
    width: 600px;
    height: 400px;
    background: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(10px);
    z-index: 2;
    position: relative;
  }

  .login-header {
    text-align: center;
    margin-bottom: 30px;

    h2 {
      color: #333;
      font-size: 24px;
      font-weight: 500;
      margin: 20px 0;
    }
  }

  .login-form {
    .login-input {
      height: 45px;
      margin: 10px 0;
      font-size: 16px;
      border-radius: 5px;
    }
  }

  .login-btn {
    width: 100%;
    height: 45px;
    font-size: 16px;
    margin-top: 20px;
    border-radius: 20px;
    background: #00bfff;
    border-color: #00bfff;

    &:hover {
      background: #1e90ff;
      border-color: #1e90ff;
    }
  }
}
</style>
